package com.xjtu.controller.admin;

import com.xjtu.dto.*;
import com.xjtu.entity.User;
import com.xjtu.result.PageResult;
import com.xjtu.result.Result;
import com.xjtu.service.UserService;
import com.xjtu.vo.UserLoginVo;
import com.xjtu.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@RequestMapping("/admin/user")
@Slf4j
@Api(tags = "管理员端用户接口")
public class AdminController {

    @Autowired
    private UserService userService;
    @PostMapping("/login")
    @ApiOperation("网页端的登陆")
    private Result<UserLoginVo> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("当前登陆的用户为{}",userLoginDTO);
        UserLoginVo userLoginVo = userService.login(userLoginDTO);
        return Result.success(userLoginVo);
    }

    @PostMapping("/logout")
    @ApiOperation("退出登陆")
    private Result logout(){
        return Result.success();
    }

    @PostMapping
    @ApiOperation(("添加用户"))
    private Result<UserVo> addUser(@RequestBody @Valid UserDto userDto){
        UserVo userVo = userService.insert(userDto);
        return Result.success(userVo);
    }

    @PutMapping
    @ApiOperation("修改用户信息")
    private Result<UserVo> editUser(@RequestBody @Valid EditUserDto editUserDto){
        UserVo userVo = userService.edit(editUserDto);
        return Result.success(userVo);
    }

    @PostMapping("/pass")
    @ApiOperation("修改用户密码")
    private Result editPass(@RequestBody @Valid EditPassDto editPassDto){
        userService.editPass(editPassDto);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("分页查询用户信息")
    public Result<PageResult> getMedicalVisitList(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                            @RequestParam(required = false, defaultValue = "10")Integer size,
                                                            @RequestParam(required = false, defaultValue = "null") String name){
        PageResult resp = userService.pageQuery(page,size,name);
        return Result.success(resp);
    }


    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    private Result deleteUser(@PathVariable Integer id){
        userService.detelt(id);
        return Result.success(null);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取用户")
    private Result<User> queryById(@PathVariable Integer id){
        User userVo = userService.getById(id);
        return Result.success(userVo);
    }
}
